﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;
using IrisGenerator.Common.Core.UI;
using IrisGenerator.Common.Tools;
using System.Drawing;
using IrisGenerator.Common.Core;

namespace IrisGenerator.Common.UI.XmlConfiguration
{
    class XmlListConfiguration:XmlConfiguration
    {
        //HACK: Modified for testing
        public override void LoadConfiguration(XElement rootElement, Control ctrl)
        {
            if (rootElement == null || ctrl == null)
                return;

            ExtendedListView list = ctrl as ExtendedListView;

            if (list == null)
                throw new ApplicationException("The control " + ctrl.Name + " is not a list. Configuration data can not be loaded");

            //FillObjectFromXmlAttributes(rootElement, list);

            XmlConfiguration.XmlToObject(rootElement,this);


            XElement columnsNode = XmlLinq.LoadSingleDescendantNode(rootElement, "Columns");
            LoadColumns(columnsNode, list);

            XElement imagesNode = XmlLinq.LoadSingleDescendantNode(rootElement, "Images");
            LoadImages(imagesNode, list);

            XElement groupsNode = XmlLinq.LoadSingleDescendantNode(rootElement, "Groups");
            LoadGroups(groupsNode, list);

            XElement itemsNode = XmlLinq.LoadSingleDescendantNode(rootElement, "ListItemTemplate");
            LoadItems(itemsNode, list);
            
        }

        private void LoadGroups(XElement groupsNode, ExtendedListView list)
        {
            if (groupsNode == null)
                return;

            var dbQuery = from node in groupsNode.Descendants()
                          select node
                          ;
            
            foreach (var item in dbQuery)
            {
                LoadGroup(item, list);
            }
        }

        private void LoadGroup(XElement item, ExtendedListView list)
        {
            if (item == null || list == null)
                return;

            AddGroup(item, list);

        }

        private void AddGroup(XElement item, ExtendedListView list)
        {
            ListViewGroup lvg = new ListViewGroup();
            FillObjectFromXmlAttributes(item, lvg);
            
            list.Groups.Add(lvg);
        }

        #region Images

        private void LoadImages(XElement imagesNode, ExtendedListView list)
        {
            if (imagesNode == null)
                return;

            
            var dbQuery = from node in imagesNode.Descendants()
                          select node
                          ;
            list.LargeImageList= new ImageList();
            list.LargeImageList.ImageSize = new Size(64,64);
            SizeConverter sc = new SizeConverter();
            
            foreach (var item in dbQuery)
            {
                LoadImage(item, list);
            }
        }

        private void LoadImage(XElement item, ExtendedListView list)
        {
            if (item == null)
                return;
            AddImage(item, list);
        }

        private void AddImage(XElement item, ExtendedListView list)
        {
            IconItem img = new IconItem();
            FillObjectFromXmlAttributes(item, img);
            if (img.Image != null)
                list.LargeImageList.Images.Add(img.Image);
        }

        #endregion

        #region Columns

        private void LoadColumns(XElement columnsNode, ExtendedListView list)
        {
            if (columnsNode == null)
                return;

            var dbQuery = from node in columnsNode.Descendants()
                          select node
                          ;

            foreach (var columnNode in dbQuery)
            {
                LoadColumn(columnNode, list);
            }



        }

        private void LoadColumn(XElement columnNode, ExtendedListView list)
        {
            if (columnNode == null)
                return;

            AddColumn(columnNode, list);
        }

        private void AddColumn(XElement columnNode, ExtendedListView list)
        {
            ColumnHeader ch = new ColumnHeader();
            list.Columns.Add(ch);
            FillObjectFromXmlAttributes(columnNode, ch);
        }

        #endregion

        #region Items

        private void LoadItems(XElement itemsNode, ExtendedListView list)
        {
            if (itemsNode == null  || list==null)
                return;

            list.Template = new ListTemplate();

            XElement listItemTemplateNode = XmlLinq.LoadSingleDescendantNode
                (itemsNode, "ItemTemplate");

            if (listItemTemplateNode != null)
            {
                list.Template.ItemTemplate=LoadItemTemplate(listItemTemplateNode);
            }

            XElement listAlternateItemTemplateNode = XmlLinq.LoadSingleDescendantNode
                (itemsNode, "AlternateItemTemplate");

            if (listAlternateItemTemplateNode != null)
            {
                list.Template.AlternateItemTemplate = LoadItemTemplate(listAlternateItemTemplateNode);
            }

            //XElement listItemTemplateNode = XmlLinq.LoadSingleDescendantNode
            //    (itemsNode, "AlternateItemTemplate");

            //var dbQuery = from node in itemsNode.Descendants("ItemTemplate/Item")
            //              select node
            //              ;

            

            //foreach (var itemNode in dbQuery)
            //{
            //    LoadItemTemplate(itemNode, list);
            //}



        }

        private ListTemplate.ListItemTemplate LoadItemTemplate(XElement itemTemplateNode)
        {
            if (itemTemplateNode == null)
                return null;

            XElement itemNode = XmlLinq.LoadSingleDescendantNode(itemTemplateNode, "Item");
            ListTemplate.ListItemTemplate template = null;
            if (itemNode != null)
            {
                template = new ListTemplate.ListItemTemplate();
                FillObjectFromXmlAttributes(itemNode, template);
                LoadSubItemsTemplate(itemNode, template);
                LoadItemKeys(itemNode, template);
            }
            return template;
        }

        private void LoadSubItemsTemplate(XElement itemNode, ListTemplate.ListItemTemplate template)
        {
            if (itemNode == null || template == null)
                return;

            template.SubItemTemplates.Clear();

            var dbQuery = from node in itemNode.Descendants("SubItem")
                          select node
                          ;

            foreach (var subNode in dbQuery)
            {
                ListTemplate.ListSubItemTemplate subItem = new ListTemplate.ListSubItemTemplate();
                template.SubItemTemplates.Add(subItem);
                FillObjectFromXmlAttributes(subNode, subItem);
            }
        }

        private void LoadItemKeys(XElement itemNode, ListTemplate.ListItemTemplate template)
        {
            if (itemNode == null || template == null)
                return;

            template.Keys.Clear();

            var dbQuery = from node in itemNode.Descendants("Key")
                          select node
                          ;

            foreach (var keyNode in dbQuery)
            {
                KeyValueObject<string, object> key = new KeyValueObject<string, object>();
                FillObjectFromXmlAttributes(keyNode, key);
                template.Keys.Add(key.Key,key.Value);
            }
        }

        private ListItem GetItemTemplate(XElement itemNode, ExtendedListView list)
        {
            if (itemNode == null || list == null || list.Template == null
                || list.Template.ItemTemplate == null)
                return null;
            
            FillObjectFromXmlAttributes(itemNode, list.Template.ItemTemplate);


            return null;
        }

        

        #endregion

    }
}
